package com.zx.base.http;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;

public class HttpUtils {
	
	private static int timeoutSocket = 10000;

	// apache http post
	public static String httpPost(String url, Map<String, String> params) {
		StringBuilder builder = new StringBuilder();
		// HttpClient client = MyHttpClient.getNewInstance();
		HttpClient client = new DefaultHttpClient();
		HttpParams httpParams = client.getParams();
		HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
		// HttpConnectionParams.setSoTimeout(params, 5000);

		HttpPost httpPost = new HttpPost(url);

		try {

			// 设置参数
			List<NameValuePair> vaules = new ArrayList<NameValuePair>();
			for (String key : params.keySet()) {
				vaules.add(new BasicNameValuePair(key, params.get(key)));
			}

			// 设置字符集
			HttpEntity entity = new UrlEncodedFormEntity(vaules, HTTP.UTF_8);
			// 设置参数实体
			httpPost.setEntity(entity);

			HttpResponse response = client.execute(httpPost);
			int statusCode = response.getStatusLine().getStatusCode();
			if (statusCode == 200) {
				HttpEntity ent = response.getEntity();
				InputStream content = ent.getContent();
				BufferedReader reader = new BufferedReader(
						new InputStreamReader(content));
				String line;
				while ((line = reader.readLine()) != null) {
					builder.append(line);
				}
			} else {
				Log.e(HttpUtils.class.toString(), "Failed to download file");
			}
		}catch(Exception e){
			return "timeout";
		}
		return builder.toString();
	}
	
	public static String readHttp(String actionUrl, Map<String, String> fields) {
//		System.out.println("WebUtils-->httpPost,actionUrl=" + actionUrl);
		String strResult = null;
		DefaultHttpClient httpClient = new DefaultHttpClient();
		try {
			HttpPost httpPost = new HttpPost(actionUrl);
			HttpParams httpParams = new BasicHttpParams();
			List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();

			// 璁剧疆瓒呮椂
			httpClient.getParams().setParameter(
					CoreConnectionPNames.CONNECTION_TIMEOUT, timeoutSocket);
			httpClient.getParams().setParameter(
					CoreConnectionPNames.SO_TIMEOUT, timeoutSocket);
			httpClient.getParams().setParameter(CoreConnectionPNames.MAX_HEADER_COUNT, timeoutSocket);
			if (null != fields) {
				for (Map.Entry<String, String> entry : fields.entrySet()) {
					nameValuePair.add(new BasicNameValuePair(entry.getKey(),
							entry.getValue()));
				}
			}
			httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair,
					HTTP.UTF_8));
			httpPost.setParams(httpParams);

			HttpResponse httpResponse = httpClient.execute(httpPost);

			if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				// 鍙栧緱杩斿洖鐨勫瓧绗︿覆
				strResult = EntityUtils.toString(httpResponse.getEntity(),
						HTTP.UTF_8);
			}
		}catch (Exception e) {
			e.printStackTrace();
			strResult = "timeout";
		}
		httpClient.getConnectionManager().shutdown();
		return strResult;
	}

	public static boolean checkConnectivity(ConnectivityManager con) {
		ConnectivityManager connectionManager = con;
		try {
			if (connectionManager.getActiveNetworkInfo().isConnected()) {
				return true;
			} else {
				return false;
			}
		} catch (NullPointerException e) {
			return false;
		}
	}

	public static boolean checkWifi(Activity activitiy) {
		WifiManager mWifiManager = (WifiManager) activitiy
				.getSystemService(Context.WIFI_SERVICE);
		WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
		int ipAddress = wifiInfo == null ? 0 : wifiInfo.getIpAddress();
		if (mWifiManager.isWifiEnabled() && ipAddress != 0) {
//			System.out.println("**** WIFI is on");
			return true;
		} else {
//			System.out.println("**** WIFI is off");
			return false;
		}
	}
}
